import Fetch, { Model } from '@wattjs/network'
// import Auth from '@wattjs/auth'
// import Store from '@wattjs/store'

// 根据项目信息上报
const Http = new Fetch({
  config: {
    APP_ID: '__UNI__55B0695', // Store.get('config').APP_ID
    BASE_URL: 'https://api.jsvue.cn',
  }
})

const service = Model(Http, {
  // url: 'https://api.jsvue.cn/com-api/petch',
  url: 'https://api.pjpiao.com/openapi/monitor',
  method: 'post'
})

const fetch = async (values) => {
  const dataParam = values.map(value => {
    const { id, type, data, stime } = value
    const dataObj = typeof data === 'string' && data.indexOf('{') > -1 ? JSON.parse(data) : data

    const { OP_ID, OP_NAME } = {} // Auth.get()
    return {
      event_type: type,
      event_params: dataObj,
      user: {
        op_id: OP_ID,
        op_name: OP_NAME,
      },
      local_time: stime
    }
  })

  try {
    return await service.send({ data: dataParam })
  } catch (error) {
    // 上报失败
    Promise.reject(error)
  }
}

export default fetch
